Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Commit 7f0e2627d1d0cee447293be67ce2283f8e5c3c9d


Parents : 29c7917
Author : Jeremy O'Brien <neutral@fastmail.com>
Date : 2026-05-20T09:43:35-04:00

atomically write lxmf message files to prevent reading of partial files from other processes during the write

Changes

1 files changed, 14 insertions(+), 3 deletions(-)


Diff

diff --git a/LXMF/LXMessage.py b/LXMF/LXMessage.py
index f85f3b8..5271d4e 100644
--- a/LXMF/LXMessage.py
+++ b/LXMF/LXMessage.py
@@ -672,15 +672,26 @@ class LXMessage:
def write_to_directory(self, directory_path):
file_name = RNS.hexrep(self.hash, delimit=False)
file_path = directory_path+"/"+file_name
+ tmp_path = file_path+".tmp."+str(os.getpid())
try:
- file = open(file_path, "wb")
- file.write(self.packed_container())
- file.close()
+ with open(tmp_path, "wb") as file:
+ file.write(self.packed_container())
+ file.flush()
+ try:
+ os.fsync(file.fileno())
+ except OSError:
+ pass
+ os.replace(tmp_path, file_path)
return file_path
except Exception as e:
+ try:
+ if os.path.exists(tmp_path):
+ os.unlink(tmp_path)
+ except Exception:
+ pass
RNS.log("Error while writing LXMF message to file \""+str(file_path)+"\". The contained exception was: "+str(e), RNS.LOG_ERROR)
return None


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────